<template>
	<view>
		<view class="header"></view>

		<view class="left-box">
			<!-- 非H5端ref不能获取内置组件 只能获取自定义组件 -->
			<view :class="{actived:index==number}" class="left-box-text" style="height: 100rpx;width: auto;"
				v-for="(item,index) in cateList" @click="changedParentSort(index,item.name)">
				<text>
					{{item.name}}
				</text>
			</view>
		</view>

		<view class="right-box">
			<scroll-view scroll-y style="height: 100%;">
				<view v-for="(item,index) in goodsList">
					<uni-row class="row">
						<uni-col :span="9">
							<view @click="clickGoods(item._id)">
								<image class="goods-img" :src="item.goods_banner_imgs[0].url"></image>
							</view>
						</uni-col>
						<uni-col :span="15">
							<view class="goods-desc-box" @click="clickGoods(item._id)">
								<p>
									{{item.name}}
								</p>
								<text>
									{{item.goods_desc}}
								</text>
							</view>
							<view class="price-box">
								<text class="price-font">
									<text class="yuan-font">￥</text>
									{{convert(item.goods_price).integer}}.
									<text class="price-font" style="font-size: 32rpx;">
										{{convert(item.goods_price).point}}
									</text>
								</text>
							</view>
							<uni-icons type="cart" size="30" class="addCart" @click="addShoppingCart(item._id)"
								color="#FF5000"></uni-icons>
						</uni-col>
					</uni-row>
					<view style="height: 10px;"></view>
				</view>
				<view style="height: 100px;"></view>
			</scroll-view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				cateList: [],
				goodsList: [],
				number: 0,
				lastDate: null,
				cate: '蔬菜'
			}
		},
		mounted() {
			this.getParentSortList()
		},
		onLoad(option) {
			if (option.cate != null && option.index != null) {
				this.changedParentSort(option.index, option.cate)
			} else {
				this.changedParentSort(0, '特色锅底')
			}
		},
		onReachBottom() {
			console.log('触底了');
		},
		// 下拉刷新
		onPullDownRefresh() {
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		methods: {
			convert(money) {
				return {
					integer: Math.floor(money / 100),
					point: money % 100
				}
			},
			async getParentSortList() {
				let res = await uniCloud.importObject('goods').getByParentsort()
				this.cateList = res;
			},
			async changedParentSort(i, cate) {
				this.cate = cate
				this.number = i

				this.goodsList = []
				let res = await uniCloud.importObject('goods').tabChanged(cate, this.lastDate)
				this.lastDate = res[res.length - 1].auto_createdate
				this.goodsList = res
			},
			getCateLastDate(date) {
				this.lastDate = parseInt(date)
			},
			//添加购物车
			addShoppingCart(id) {
				const db = uniCloud.database();
				db.collection('shop-cart').add({
					'goods_id': id
				}).then(res => {
					uni.showToast({
						title: '添加成功！',
						icon: 'success',
						duration: 800
					})
				}).catch(e => {
					uni.showToast({
						title: '不能重复添加！',
						icon: 'error',
						duration: 800
					})
				})
			},
			async clickGoods(id) {
				let res = await uniCloud.importObject('goods').getById(id)
				uni.navigateTo({
					url: '/pages/details/details?item=' + encodeURIComponent(JSON.stringify(res[0]))
				})
			},
		},

	}
</script>

<style scoped>
	.left-box {
		left: 0;
		top: 0;
		position: fixed;
		height: 100%;
		margin-top: 100rpx;
		background-color: #f9f9fc;
		z-index: 1;
		border: 1px solid #fff;
		width: 170rpx
	}


	.right-box {
		position: fixed;
		right: 0;
		height: 100%;
		margin-top: 100rpx;
		width: 580rpx;
	}

	.header {
		width: 100%;
		height: 95rpx;
		background-color: #f9f9fc;
		position: fixed;
		top: 0px;
		z-index: 2;
	}

	.actived {
		/*  !important 提高优先级 */
		background: #ffffff !important;
		font-weight: bold;
	}

	.left-box-text {
		display: flex;
		align-items: center;
		justify-content: center;
		border-bottom: white 2px solid;
	}

	.goods-desc-box {
		height: 70%;
		width: 85%;
		word-break: break-all;
		overflow: hidden;
		display: -webkit-box;
		-webkit-line-clamp: 3;
		-webkit-box-orient: vertical;
	}

	.goods-desc-box text {
		font-size: 12px;
		color: #a2a2a2;
	}

	.goods-img {
		height: 200rpx;
		width: 200rpx;
		border-radius: 8px
	}

	.row {
		height: 200rpx;
		border-radius: 18rpx;
		background-color: #ffffff;
	}

	.price-font {
		font-size: 40rpx;
		color: #FF5000;
		font-family: Verdana;
	}

	.title-font {
		font-size: Tahoma;
		font-weight: bold;
	}

	.yuan-font {
		color: #FF5000;
		font-size: 32rpx;
	}

	.price-box {
		height: 30%;
		width: 85%;
		word-wrap: break-word;
		position: absolute;
		bottom: 0rpx;
	}

	.addCart {
		height: 60rpx;
		width: 60rpx;
		position: absolute;
		bottom: 10rpx;
		right: 10rpx;
	}
</style>